package model

import (
	"context"
	"portal/internal/types"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ AwNewsModel = (*customAwNewsModel)(nil)

type (
	// AwNewsModel is an interface to be customized, add more methods here,
	// and implement the added methods in customAwNewsModel.
	AwNewsModel interface {
		awNewsModel
		withSession(session sqlx.Session) AwNewsModel
		GetListByCondition(ctx context.Context, session sqlx.Session, page types.PageParamRequest,news *types.News)([]*AwNewsInc,error)
		GetListAboutRecom(ctx context.Context,session sqlx.Session,page types.PageParamRequest)([]*AwNewsInc,error)
		GetCount(ctx context.Context,session sqlx.Session,news *types.News) (int64, error)
		GetDetailOne(ctx context.Context, session sqlx.Session, newsPath string)(*AwNewsInc,error)
	}

	customAwNewsModel struct {
		*defaultAwNewsModel
	}
)

// NewAwNewsModel returns a model for the database table.
func NewAwNewsModel(conn sqlx.SqlConn) AwNewsModel {
	return &customAwNewsModel{
		defaultAwNewsModel: newAwNewsModel(conn),
	}
}

func (m *customAwNewsModel) withSession(session sqlx.Session) AwNewsModel {
	return NewAwNewsModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customAwNewsModel)GetListByCondition(ctx context.Context, session sqlx.Session, page types.PageParamRequest,news *types.News)([]*AwNewsInc,error){
	sb := squirrel.Select("news.*,nt.type_name as typename").From(m.table + " news")
	if news.IsHot {
		sb = sb.Where(squirrel.Eq{"is_hot":1})
	}
	if news.PublishTime != 0 {
		sb = sb.Where(squirrel.Eq{"publish_time":news.PublishTime})
	}
	if news.Type != 0 {
		sb = sb.Where(squirrel.Eq{"type":news.Type})
	}
	sb = sb.Where(squirrel.Eq{"state":1})
	sb = sb.InnerJoin("`aw_news_types` nt on nt.type_id = type")
	sb = sb.Offset(uint64((page.PageNum - 1 )*page.PageSize)).Limit(uint64(page.PageSize))
	sb = sb.OrderBy("update_time DESC")
	query,args,err := sb.ToSql()
	if err != nil {
		return nil,err
	}
	var newsInc []*AwNewsInc
	if session != nil {
		err = session.QueryRowsPartialCtx(ctx,&newsInc,query,args...)
	}else{
		err = m.conn.QueryRowsPartialCtx(ctx,&newsInc,query,args...)
	}
	return newsInc,err
}

func (m *customAwNewsModel)GetListAboutRecom(ctx context.Context,session sqlx.Session,page types.PageParamRequest)([]*AwNewsInc,error){
	sb := squirrel.Select("news.*,nt.type_name as typename").From("`aw_recommend_news` rn")
	sb = sb.InnerJoin("`aw_news` news on news.news_id = rn.news_id")
	sb = sb.InnerJoin("`aw_news_types` nt on nt.type_id = news.type")
	sb = sb.Where(squirrel.Eq{"state":1})
	sb = sb.Offset(uint64((page.PageNum - 1 )*page.PageSize)).Limit(uint64(page.PageSize))
	sb = sb.OrderBy("publish_time DESC")
	query,args,err := sb.ToSql()
	if err != nil {
		return nil,err
	}
	var newsInc []*AwNewsInc
	if session != nil {
		err = session.QueryRowsPartialCtx(ctx,&newsInc,query,args...)
	}else{
		err = m.conn.QueryRowsPartialCtx(ctx,&newsInc,query,args...)
	}
	return newsInc,err
}

func (m *customAwNewsModel)GetCount(ctx context.Context,session sqlx.Session,news *types.News) (int64, error){
	sb := squirrel.Select("count(1)").From(m.table)
	if news.PublishTime != 0 {
		sb = sb.Where(squirrel.Eq{"publish_time":news.PublishTime})
	}
	if news.Type != 0 {
		sb = sb.Where(squirrel.Eq{"type":news.Type})
	}
	sb = sb.Where(squirrel.Eq{"state":1})
	query,args,err := sb.ToSql()
	if err != nil {
		return 0,err
	}
	var cut int64
	if session != nil {
		err = session.QueryRowPartialCtx(ctx,&cut,query,args...)
	}else{
		err = m.conn.QueryRowPartialCtx(ctx,&cut,query,args...)
	}
	return cut,err
}


func (m *customAwNewsModel)GetDetailOne(ctx context.Context, session sqlx.Session, newsPath string)(*AwNewsInc,error){
	sb := squirrel.Select("news.*,nt.type_name as typename").From(m.table + " news")
	sb = sb.Where(squirrel.Eq{"state":1})
	sb = sb.Where(squirrel.Eq{"news_path":newsPath})
	sb = sb.InnerJoin("`aw_news_types` nt on nt.type_id = type")
	sb = sb.Limit(1)
	query,args,err := sb.ToSql()
	if err != nil {
		return nil,err
	}
	var newsInc AwNewsInc
	if session != nil {
		err = session.QueryRowCtx(ctx,&newsInc,query,args...)
	}else{
		err = m.conn.QueryRowCtx(ctx,&newsInc,query,args...)
	}
	return &newsInc,err
}